import { Component, OnInit, ViewChildren, QueryList } from '@angular/core';
import { TurboperHistory } from "../../../entity/turboper-history.model";
import { TurboperCategory } from "../../../entity/turboper-category.model";
import { ApplicationService } from "../../../service/application.service";
import { TurboperCategoryService } from "../../../service/turboper-category.service";
import { TurboperHistoryService } from "../../../service/turboper-history.service"
import { Farm } from "../../../entity/farm.model";
import { OverhaulTableComponent } from "../farm-overhaul/overhaul-table/overhaul-table.component"
import { Device } from '../../../entity/device.model';
import { FarmCategoryEnum } from '../../common/device-tag-selector/farm-category.enum';
import { DeviceCategoryEnum } from '../../common/device-tag-selector/device-category.enum';

@Component({
  selector: 'app-disease-operation',
  templateUrl: './disease-operation.component.html',
  styleUrls: ['./disease-operation.component.css']
})
/* ngModelChange */
export class DiseaseOperationComponent implements OnInit {
  public farmCategory = FarmCategoryEnum.winfarm;
  public deviceCategory: DeviceCategoryEnum[] = [DeviceCategoryEnum.turbine];
  @ViewChildren(OverhaulTableComponent)
  overhaultables: QueryList<OverhaulTableComponent>;
  turboperCategorys: TurboperCategory[] = [];
  public active = 0;
  turboperHistorys: TurboperHistory[] = [];
  farmResultTurbHis: TurboperHistory[] = [];
  farmTurboperHistorys: TurboperHistory[] = [];
  currentTurboperHistorys: TurboperHistory[] = [];
  currentResultTurbHis: TurboperHistory[] = [];
  companyTurboperHistorys: TurboperHistory[] = [];
  companyResultTurbHis: TurboperHistory[] = [];
  farms: Farm[] = [];
  currentFarmId: number;
  /**
   * 结束时间
   * 
   * @type {Date}
   * @memberof DiseaseOperationComponent
   */
  endDate: Date = new Date();

  /**
   * 开始时间
   * 
   * @type {Date}
   * @memberof DiseaseOperationComponent
   */
  startDate: Date = new Date(this.endDate.getFullYear(), this.endDate.getMonth());

  /**
   * 当前选中的数据
   * 
   * @type {TurboperHistory}
   * @memberof DiseaseOperationComponent
   */
  currentHistory: TurboperHistory;
  display: boolean = false;
  mes: string;
  isEdit: boolean = false;
  deviceIdList: number[];
  timer: any = null;
  complete: boolean = false;
  notComplete: boolean = false;
  /**
   *设备选择最大数量
   *
   * @type {number}
   * @memberof DiseaseOperationComponent
   */
  deviceCountLimit: number;
  /**
   *日期格式化
   *
   * @type {*}
   * @memberof DiseaseOperationComponent
   */
  en:any;
  constructor(
    private application: ApplicationService,
    private turboperHistSer: TurboperHistoryService,
    private turbocateSer: TurboperCategoryService,
  ) { }

  ngOnInit() {
    this.en = {
			firstDayOfWeek: 0,
			dayNames: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
			dayNamesShort: ['日', '一', '二', '三', '四', '五', '六'],
			dayNamesMin: ['日', '一', '二', '三', '四', '五', '六'],
			monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
			monthNamesShort: ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二'],
			today: '今天',
			clear: '清除'
		};
    if (this.application.currentMenuParams && this.application.currentMenuParams['deviceCountLimit']) {
      this.deviceCountLimit = Number(this.application.currentMenuParams['deviceCountLimit'].paramValue);
    } else {
      this.deviceCountLimit = null;
    }
    this.farms = this.application.currentUserFarms;
    this.currentFarmId = this.farms[0].farmId;
    this.turbocateSer.getTurboperCategorysByGeneralCategory("03")
      .then(res => {
        if (!res) { return false; };
        this.turboperCategorys = [...res]
      })
      .catch(err => {
        console.log(err)
      })
    this.getData()

  }


  /**
   * 风机选择返回的选中风机集合
   * 
   * @param {any} ev 
   * @memberof FarmOverhaulComponent
   */
  onSelectedDevicesChange(ev: Device[]) {
    clearTimeout(this.timer)
    this.timer = setTimeout(() => {
      let deviceIdList: number[] = [];
      deviceIdList.push(...ev.map(device => device.deviceId))

      this.deviceIdList = deviceIdList;
      this.getDataByTurbineIds();
    }, 800)

  }
  getDataByTurbineIds() {
    this.turboperHistSer.getTurboperHistByTurbines(this.deviceIdList, this.startDate, this.endDate, "03")
      .then(res => {
        if (!res) { return false; };
        this.farmResultTurbHis = [...res];
        this.farmTurboperHistorys = [...res]
      })
  }
  getData() {
    //当前待检
    this.turboperHistSer.getTurboperHistByType()
      .then(res => {
        if (!res) { return false; };
        this.currentTurboperHistorys = [...res]
        this.currentResultTurbHis = [...res]
      })
      .catch(err => {
        console.log(err)
      })
    //在时间段里的数据
    this.turboperHistSer.getTurboperHistByTimeAndType(this.startDate, this.endDate, "03")
      .then(res => {
        if (!res) { return false; };
        this.companyTurboperHistorys = [...res]
        this.companyResultTurbHis = [...res]
      })
      .catch(err => {
        console.log(err);
      })
  }
  onFarmClick(item: Farm) {
    this.currentFarmId = item.farmId;

  }
  onHeaerClick(n: number) {
    this.active = n;

  }

  onSelectHistory(ev) {
    this.currentHistory = ev;

    if (!this.currentHistory) {
      this.mes = "没有选中数据"
      return this.display = true;
    }
    this.isEdit = true;

  }
  onSearch() {
    this.getDataByTurbineIds();
    this.getData()
  }
  onEdit() {

    this.overhaultables.forEach((o, i) => {

      if (i == this.active) {
        o.emitSelector()
      }
    })
  }
  onColseEdit(ev) {
    this.isEdit = false;
    if (ev) {
      this.mes = "数据更新成功"
      this.display = true;
      this.onSearch();
    }
  }

  onBoxChange(ev: any, item?: TurboperCategory) {
    item.isChecked = ev.checked;
    this.getTurboperHisByoperId()

  }
  onCompleteChange(ev: any) {
    if ((this.complete && this.notComplete) || (!this.complete && !this.notComplete)) {
      this.companyTurboperHistorys = [...this.companyResultTurbHis];
      this.farmTurboperHistorys = [...this.farmResultTurbHis];

    } else if (this.complete && !this.notComplete) {
      this.companyTurboperHistorys = this.companyResultTurbHis.filter(o => o.operEndTime)
      this.farmTurboperHistorys = this.farmResultTurbHis.filter(o => o.operEndTime);

    } else if (!this.complete && this.notComplete) {
      this.companyTurboperHistorys = this.companyResultTurbHis.filter(o => !o.operEndTime)
      this.farmTurboperHistorys = this.farmResultTurbHis.filter(o => !o.operEndTime);
    }
    this.getTurboperHisByoperId()


  }
  getTurboperHisByoperId() {
    let companyTurboperHistorys = [];
    let farmTurboperHistorys = [];
    let currentTurboperHistorys = [];
    let isChecked: Boolean;
    this.turboperCategorys.forEach(turboper => {
      if (turboper.isChecked) {
        isChecked = true
        companyTurboperHistorys = companyTurboperHistorys.concat(this.companyTurboperHistorys.filter(o => o.operCategoryId == turboper.operCategoryId));
        farmTurboperHistorys = farmTurboperHistorys.concat(this.farmTurboperHistorys.filter(o => o.operCategoryId == turboper.operCategoryId));
        currentTurboperHistorys = currentTurboperHistorys.concat(this.currentResultTurbHis.filter(o => o.operCategoryId == turboper.operCategoryId));
      }
    })
    if (isChecked) {
      this.companyTurboperHistorys = [...companyTurboperHistorys];
      this.farmTurboperHistorys = [...farmTurboperHistorys];
      this.currentTurboperHistorys = [...currentTurboperHistorys];
    }
  }
}



// WEBPACK FOOTER //
// ./src/app/module/data-analysis/disease-operation/disease-operation.component.ts